package gui;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComboBox;

import mvc.CurveModelManager;
import mvc.bezier.BezierBlock;
import mvc.helix.HelixBlock;
import mvc.viviani.VivianiBlock;


public class TrunkTab extends JTitledPanel implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//-------------------------------------------------------- combo box 
	//-------------------------------------------------------- menu
	//-------------------------------------------------------- stuffs
	String[] trunkNames = { 
			"Bezier", 
			"Helix", 
			"Viviani",
			"todo", 
			"nada", 
			"blabla" };
	
	Map<String, Integer> map = new HashMap<String, Integer>() {private static final long serialVersionUID = 1L;
	{
		put("Bezier", 1);
		put("Helix", 2);
		put("Viviani", 3);
	}};
	
	//-------------------------------------------------------- classvars
	private CurveModelManager curvemanager;

	private Block current;
	
	// all possible blocks containing the curve that could be used as trunk
	private Block bezier = null;
	private Block helix = null;
	private Block viviani = null;

	
	//-------------------------------------------------------- constructor
	//-------------------------------------------------------- init
	public TrunkTab(String title) {
		super(title);
		
		initSwing();
		
		curvemanager = CurveModelManager.getInstance();
		
		addDefaultPanel( getBezierBlock() );
	}

	private void initSwing() {
		setLayout(new BorderLayout());

		JComboBox c = new JComboBox(trunkNames);
		c.addActionListener(this);
		add(c,BorderLayout.PAGE_START);
	}

	//-------------------------------------------------------- public
	//-------------------------------------------------------- add Default Panel
	public void addDefaultPanel(Block block) {
		current = block;
		add(block);
		updateUI();
	}

	//-------------------------------------------------------- checkbox controller
	//-------------------------------------------------------- Action Event listener
	@Override
	public void actionPerformed(ActionEvent e) {
		JComboBox cb = (JComboBox)e.getSource();
		String trunkName = (String)cb.getSelectedItem();
		updatePanel(trunkName);
	}

	private void updatePanel(String trunkName) {
		System.out.println("TrunkTab > updatePanel "+trunkName);
		remove(current);
		current.dispose();
		addDefaultPanel(getBlock(map.get(trunkName)));
	}
	
	
	//--------------------------------------------------------
	//-------------------------------------------------------- factory methods
	public Block getBlock(int index) {
		Block r = null;

		switch(index) {
		case CurveModelManager.BEZIER: r = getBezierBlock(); break;
		case CurveModelManager.HELIX: r = getHelixBlock(); break;
		case CurveModelManager.VIVIANI: r = getVivianiBlock(); break;
		}
		return r;
	}

	private Block getVivianiBlock() {
		viviani = new VivianiBlock("viviani", curvemanager.getViviani());
		viviani.init();
		
		return viviani;
	}

	private Block getBezierBlock() {
		bezier = new BezierBlock("bezier", curvemanager.getBezier());
		bezier.init();

		return bezier;
	}

	private Block getHelixBlock() {
		helix = new HelixBlock("helix", curvemanager.getHelix());
		helix.init();

		return helix;
	}

}
